package strategic_monitor

import (
	"context"
	"git.mycaigou.com/gfyx/common/tenant_db"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/infrastructure/common/utils"
	"gorm.io/gorm"
)

type strategicMonitorRepo struct {
	tenant_db.TenantDB
}

func NewStrategicMonitorRepo() StrategicMonitorRepoIface {
	return &strategicMonitorRepo{}
}

func (r *strategicMonitorRepo) SaveIndustryRecord(ctx context.Context, po *GfyxStrategicIndustryRecordPo) (*GfyxStrategicIndustryRecordPo, error) {
	err := r.GetTenantDB(ctx).Table(po.TableName()).Create(po).Error
	if err != nil {
		return nil, err
	}
	return po, nil
}

func (r *strategicMonitorRepo) QueryIndustryRecordList(ctx context.Context, condition map[string]interface{}, pageInfo *utils.PageInfo) (int64, []*GfyxStrategicIndustryRecordPo, error) {
	var result []*GfyxStrategicIndustryRecordPo
	var count int64
	db := r.GetTenantDB(ctx).Table(GfyxStrategicIndustryRecordPo{}.TableName())

	if condition != nil {
		if len(condition) > 0 {
			db = db.Where(condition)
		}
	}

	err := db.Count(&count).Error
	if err != nil {
		return 0, nil, err
	}

	if pageInfo != nil && pageInfo.PageSize > 0 && pageInfo.Page > 0 {
		db = db.Offset((pageInfo.Page - 1) * pageInfo.PageSize).Limit(pageInfo.PageSize)
	}

	err = db.Order("id desc").Find(&result).Error
	if err != nil {
		if err == gorm.ErrRecordNotFound {
			return 0, result, nil
		}
		return 0, nil, err
	}
	return count, result, nil
}

func (r *strategicMonitorRepo) UpdateIndustryRecord(ctx context.Context, condition, data map[string]interface{}) error {
	db := r.GetTenantDB(ctx).Table(GfyxStrategicIndustryRecordPo{}.TableName())
	if condition != nil {
		if len(condition) > 0 {
			db = db.Where(condition)
		}
	}
	return db.Updates(data).Error
}

func (r *strategicMonitorRepo) SaveIndustryMonitor(ctx context.Context, po *GfyxStrategicIndustryMonitorPo) (*GfyxStrategicIndustryMonitorPo, error) {
	err := r.GetTenantDB(ctx).Table(po.TableName()).Create(po).Error
	if err != nil {
		return nil, err
	}
	return po, nil
}

func (r *strategicMonitorRepo) QueryIndustryMonitorList(ctx context.Context, condition map[string]interface{}, pageInfo *utils.PageInfo, orderBy string) (int64, []*GfyxStrategicIndustryMonitorPo, error) {
	var result []*GfyxStrategicIndustryMonitorPo
	var count int64
	db := r.GetTenantDB(ctx).Table(GfyxStrategicIndustryMonitorPo{}.TableName())

	if condition != nil {
		if len(condition) > 0 {
			db = db.Where(condition)
		}
	}

	err := db.Count(&count).Error
	if err != nil {
		return 0, nil, err
	}

	if pageInfo != nil && pageInfo.PageSize > 0 && pageInfo.Page > 0 {
		db = db.Offset((pageInfo.Page - 1) * pageInfo.PageSize).Limit(pageInfo.PageSize)
	}

	if orderBy == "" {
		orderBy = "id desc"
	}

	err = db.Order(orderBy).Find(&result).Error
	if err != nil {
		if err == gorm.ErrRecordNotFound {
			return 0, result, nil
		}
		return 0, nil, err
	}
	return count, result, nil
}

func (r *strategicMonitorRepo) UpdateIndustryMonitor(ctx context.Context, condition, data map[string]interface{}) error {
	db := r.GetTenantDB(ctx).Table(GfyxStrategicIndustryMonitorPo{}.TableName())
	if condition != nil {
		if len(condition) > 0 {
			db = db.Where(condition)
		}
	}
	return db.Updates(data).Error
}

func (r *strategicMonitorRepo) SaveCompanyRecord(ctx context.Context, po *GfyxStrategicCompanyRecordPo) (*GfyxStrategicCompanyRecordPo, error) {
	err := r.GetTenantDB(ctx).Table(po.TableName()).Create(po).Error
	if err != nil {
		return nil, err
	}
	return po, nil
}

func (r *strategicMonitorRepo) QueryCompanyRecordList(ctx context.Context, condition map[string]interface{}, pageInfo *utils.PageInfo) (int64, []*GfyxStrategicCompanyRecordPo, error) {
	var result []*GfyxStrategicCompanyRecordPo
	var count int64
	db := r.GetTenantDB(ctx).Table(GfyxStrategicCompanyRecordPo{}.TableName())

	if condition != nil {
		if len(condition) > 0 {
			db = db.Where(condition)
		}
	}

	err := db.Count(&count).Error
	if err != nil {
		return 0, nil, err
	}

	if pageInfo != nil && pageInfo.PageSize > 0 && pageInfo.Page > 0 {
		db = db.Offset((pageInfo.Page - 1) * pageInfo.PageSize).Limit(pageInfo.PageSize)
	}

	err = db.Order("id desc").Find(&result).Error
	if err != nil {
		if err == gorm.ErrRecordNotFound {
			return 0, result, nil
		}
		return 0, nil, err
	}
	return count, result, nil
}

func (r *strategicMonitorRepo) UpdateCompanyRecord(ctx context.Context, condition, data map[string]interface{}) error {
	db := r.GetTenantDB(ctx).Table(GfyxStrategicCompanyRecordPo{}.TableName())
	if condition != nil {
		if len(condition) > 0 {
			db = db.Where(condition)
		}
	}
	return db.Updates(data).Error
}

func (r *strategicMonitorRepo) SaveCompanyMonitor(ctx context.Context, po *GfyxStrategicCompanyMonitorPo) (*GfyxStrategicCompanyMonitorPo, error) {
	err := r.GetTenantDB(ctx).Table(po.TableName()).Create(po).Error
	if err != nil {
		return nil, err
	}
	return po, nil
}

func (r *strategicMonitorRepo) QueryCompanyMonitorList(ctx context.Context, condition map[string]interface{}, pageInfo *utils.PageInfo) (int64, []*GfyxStrategicCompanyMonitorPo, error) {
	var result []*GfyxStrategicCompanyMonitorPo
	var count int64
	db := r.GetTenantDB(ctx).Table(GfyxStrategicCompanyMonitorPo{}.TableName())

	if condition != nil {
		if len(condition) > 0 {
			db = db.Where(condition)
		}
	}

	err := db.Count(&count).Error
	if err != nil {
		return 0, nil, err
	}

	if pageInfo != nil && pageInfo.PageSize > 0 && pageInfo.Page > 0 {
		db = db.Offset((pageInfo.Page - 1) * pageInfo.PageSize).Limit(pageInfo.PageSize)
	}

	err = db.Order("id desc").Find(&result).Error
	if err != nil {
		if err == gorm.ErrRecordNotFound {
			return 0, result, nil
		}
		return 0, nil, err
	}
	return count, result, nil
}

func (r *strategicMonitorRepo) UpdateCompanyMonitor(ctx context.Context, condition, data map[string]interface{}) error {
	db := r.GetTenantDB(ctx).Table(GfyxStrategicCompanyMonitorPo{}.TableName())
	if condition != nil {
		if len(condition) > 0 {
			db = db.Where(condition)
		}
	}
	return db.Updates(data).Error
}
